package key.w8.boot.config;

import com.k8.common.factroy.BeanFactory;
import com.k8.common.util.StrUtil;
import com.k8.registry.RegistryManager;
import key.w8.boot.condition.OnK8ServiceEnabledCondition;
import key.w8.boot.listener.ApplicationStartListener;
import key.w8.boot.properties.ProviderAutoConfigurationProperties;
import key.w8.boot.service.ServiceFactory;
import key.w8.boot.service.ServiceRegistry;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

import static com.k8.common.constants.SpiConstants.PROXY_TYPE_JAVASSIST;

/**
 * @Author: k8
 * @CreateTime: 2025-08-17
 * @Version: 1.0
 */
@Configuration
@Conditional(OnK8ServiceEnabledCondition.class)
@EnableConfigurationProperties(ProviderAutoConfigurationProperties.class)
public class K8ServiceAutoConfiguration {
    @Bean
    public BeanFactory providerBeanFactory(ProviderAutoConfigurationProperties providerAutoConfigurationProperties){
        String proxy = providerAutoConfigurationProperties.getProxy();
        if (StrUtil.isEmpty(proxy)){
            proxy = PROXY_TYPE_JAVASSIST;
        }
        BeanFactory beanFactory = new BeanFactory(proxy);
        beanFactory.getOrRegistryBean(RegistryManager.class);
        return beanFactory;
    }



    @Bean
    public ApplicationStartListener applicationStartListener(ServiceFactory serviceFactory){
       return new ApplicationStartListener(serviceFactory);
    }

    @Bean
    public ServiceRegistry serviceRegistry(){
        return new ServiceRegistry();
    }
}
